import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:photo_manager/photo_manager.dart';
import '/app/controllers/history_manager.dart';
import '/app/routes/app_pages.dart';
import 'home_controller.dart';
import 'widgets/item_video_card.dart';

class HomePage extends StatelessWidget {
  final HomeController homeController = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: [
          // 最近播放记录标题
          Container(
            padding: EdgeInsets.fromLTRB(15, 20, 15, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "最近播放记录",
                  style: TextStyle(
                    // color: Color(0xff191d31),
                    fontSize: 15,
                    fontFamily: "Outfit",
                    fontWeight: FontWeight.w600,
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    Get.toNamed(AppPages.video_list, parameters: {"title": "最近播放记录", "contentType": "history"});
                  },
                  child: Text(
                    "查看全部",
                    style: TextStyle(
                      // color: Color(0xff8576d0),
                      fontSize: 12,
                      fontFamily: "Outfit",
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 5),
          // 最近播放记录内容
          Container(
            padding: EdgeInsets.only(left: 5),
            child: Obx(() {
              if (homeController.historyVideoList.isNotEmpty) {
                return SizedBox(
                  height: 130,
                  child: ListView.builder(
                    scrollDirection: Axis.horizontal,
                    itemCount: homeController.historyVideoList.length,
                    itemBuilder: (context, index) {
                      final video = homeController.historyVideoList[index];
                      return _buildVideoListItem(video);
                    },
                  ),
                );
              } else {
                return Text("无历史数据", style: TextStyle(fontSize: 14));
              }
            }),
          ),
          SizedBox(height: 10),
          // 播放列表标题
          Container(
            padding: EdgeInsets.fromLTRB(15, 20, 15, 0),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "播放列表",
                  style: TextStyle(
                    // color: Color(0xff191d31),
                    fontSize: 15,
                    fontFamily: "Outfit",
                    fontWeight: FontWeight.w600,
                  ),
                ),
                GestureDetector(
                  onTap: () {
                    // Get.toNamed(AppPages.playlist); // 新增：跳转到播放列表管理页
                  },
                  child: Text(
                    "查看全部",
                    style: TextStyle(
                      // color: Color(0xff8576d0),
                      fontSize: 12,
                      fontFamily: "Outfit",
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ],
            ),
          ),
          SizedBox(height: 5),
          // 播放列表内容
          // Container(
          //   padding: EdgeInsets.only(left: 5),
          //   child: Obx(() {
          //     final playlists = homeController.playlists.take(5).toList(); // 新增：显示最多5个播放列表
          //     return Row(
          //       children: playlists.map((playlist) {
          //         return _buildPlaylistCard(playlist); // 新增：播放列表卡片
          //       }).toList(),
          //     );
          //   }),
          // ),
          SizedBox(height: 50),
          GestureDetector(
            onTap: () {
              HistoryManager.clearAll();
            },
            child: Text(
              "清除历史记录",
              style: TextStyle(
                color: Color(0xff8576d0),
                fontSize: 12,
                fontFamily: "Outfit",
                fontWeight: FontWeight.w500,
              ),
            ),
          ),

          SizedBox(height: 50),
          GestureDetector(
            onTap: () {
              Get.toNamed(AppPages.play_list_page);
            },
            child: Text(
              "跳转至播放列表页面",
              style: TextStyle(
                color: Color(0xff8576d0),
                fontSize: 12,
                fontFamily: "Outfit",
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    );
  }

  // 构建视频列表项
  Widget _buildVideoListItem(AssetEntity video) {
    return FutureBuilder<Uint8List?>(
      future: video.thumbnailData,
      builder: (context, snapshot) {
        final thumbnail = snapshot.data;
        Widget imageWidget = thumbnail != null
            ? Image.memory(thumbnail, width: 64, height: 64, fit: BoxFit.cover)
            : Container(width: 64, height: 64, color: Colors.grey);

        return VideoCard(
          assetEntity: video,
          image: imageWidget,
          videoName: video.title ?? "Unknown",
          currentTime: "00:00:10",
          // 示例数据
          totalTime: "01:22:10", // 示例数据
        );
      },
    );
  }

}
